//
//  DZYTabBarController.m
//  DZYFM
//
//  Created by dongzhenyu on 2017/10/31.
//  Copyright © 2017年 dongzhenyu. All rights reserved.
//

#import "DZYTabBarController.h"
#import "DZYTabBar.h"
#import "DZYNavigationController.h"
#import "DZYMiddleView.h"

@interface DZYTabBarController ()

@end

@implementation DZYTabBarController

+ (instancetype)shareInstance
{
    static DZYTabBarController *tabBarVc;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tabBarVc = [[DZYTabBarController alloc] init];
    });
    return tabBarVc;
}

+ (instancetype)tabBarControllerWithAddChildVcBlock:(void (^)(DZYTabBarController *))addVCBlock
{
    DZYTabBarController *tabBarVc = [[DZYTabBarController alloc] init];
    if (addVCBlock) {
        addVCBlock(tabBarVc);
    }
    return tabBarVc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置tabBar
    [self setupTabBar];
}

- (void)setupTabBar
{
    [self setValue:[[DZYTabBar alloc] init] forKey:@"tabBar"];
}

/**
 *  根据参数, 创建并添加对应的子控制器
 *
 *  @param vc                需要添加的控制器(会自动包装导航控制器)
 *  @param isRequired             标题
 *  @param normalImgName   一般图片名称
 *  @param selectedImgName 选中图片名称
 */
- (void)addChildVc:(UIViewController *)vc normalImgName:(NSString *)normalImgName selectedImgName:(NSString *)selectedImgName isRequiredNavController:(BOOL)isRequired
{
    if (isRequired) {
        
        DZYNavigationController *nav = [[DZYNavigationController alloc] initWithRootViewController:vc];
        nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:[[UIImage imageNamed:normalImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  selectedImage:[[UIImage imageNamed:selectedImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [self addChildViewController:nav];
    } else {
        [self addChildViewController:vc];
    }
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    
    UIViewController *vc = self.childViewControllers[selectedIndex];
    if (vc.view.tag == 666) {
        vc.view.tag = 888;
        
        DZYMiddleView *middleView = [DZYMiddleView middleView];
        middleView.middleClickBlock = [DZYMiddleView shareInstance].middleClickBlock;
        middleView.isPlaying = [DZYMiddleView shareInstance].isPlaying;
        middleView.middleImg = [DZYMiddleView shareInstance].middleImg;
        CGRect frame = middleView.frame;
        frame.size.width = 65;
        frame.size.height = 65;
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        frame.origin.x = (screenSize.width - 65) * 0.5;
        frame.origin.y = screenSize.height - 65;
        middleView.frame = frame;
        [vc.view addSubview:middleView];
        
    }
    
    
}

@end
